import { markList } from './db/markList'

// 获取经纬度坐标
function getPoints(address, cb) {
  const myGeo = new BMap.Geocoder()
  // 将地址解析结果显示在地图上，并调整地图视野
  myGeo.getPoint(
    address,
    (point) => {
      if (point) {
        cb(point)
      }
    },
    '北京市'
  )
}

// marker绑定事件
function addMarkerEvent({ map, marker, point, mark }) {
  const opts = {
    title: '台账详情', // 信息窗口标题
  }
  var infoWindow = new BMap.InfoWindow(
    `
      <ul>
       <li>隐患地点：${mark.detail}</li>
       <li>隐患原因: ${mark.reason}</li>
      </ul>
     
    `,
    opts
  )
  marker.addEventListener('click', () => {
    map.openInfoWindow(infoWindow, point)
  })
}

// 添加Marker
function addMarker(map) {
  markList.forEach((mark) => {
    getPoints(`${mark.Belonging}${mark.detail}`, (points) => {
      const { lat, lng } = points
      const point = new BMap.Point(lng, lat)
      const marker = new BMap.Marker(point)
      map.addOverlay(marker)
      addMarkerEvent({ map, marker, point, mark })
    })
  })
}

export { addMarker }
